var Path = require('path');
const mysql = require('mysql');
const constant = require('./constant');

function query_location(file_id) {
    var Pool = mysql.createPool({
        'host':constant.DB_HOST_NAME,
        'user':constant.DB_USER,
        'password':constant.DB_PWD,
        'database':constant.DB_NAME,
        'multipleStatements': true // 支持执行多条 sql 语句
    });
    Pool.getConnection( (err,connection) => {
        if(err){
            console.log(err);
            res.send({
                'res':{
                    'ok': 0,
                    'msg':'数据库连接失败'
                }
            });
        }else{
            let  sql = "select location_array from files where file_id=?;"
            let  sqlParams = [
                file_id,
            ];
            connection.query(sql,sqlParams,async (err,data) => {
                if(err){
                    console.log(err);
                    res.statusCode = 500;
                    res.send({
                        'res':{
                            'ok': 0,
                            'msg':'数据库连接失败'
                        }
                    });
                }else{
                    let res_data = JSON.stringify(data);//把data对象转为字符串，去掉RowDataPacket
                    res_data = JSON.parse(res_data);//把results字符串转为json对象
                    // console.log(res_data);
                    if(res_data.length == 1){
                        let location_array = res_data[0].location_array.split(',');
                        let sql_array = [];
                        for(let i in location_array){
                            sql_array.push('select name from folder where fid = '+location_array[i]+';');
                        }
                        let sql_inner = sql_array.join('');
                        console.log(sql_inner);
                        connection.query(sql_inner,async (err,data) => {
                            if(err){
                                console.log(err);
                                res.statusCode = 500;
                                res.send({
                                    'res':{
                                        'ok': 0,
                                        'msg':'数据库连接失败'
                                    }
                                });
                            }else{
                                let res_data = JSON.stringify(data);//把data对象转为字符串，去掉RowDataPacket
                                res_data = JSON.parse(res_data);//把results字符串转为json对象
                                console.log(res_data);
                                let result = [];
                                if(res_data.length >= 1){
                                    for(let i = 0;i < res_data.length;i++){
                                        result.push(res_data[i][0].name);
                                    }
                                    res.send({
                                        res:{
                                            ok:1,
                                            msg:"查找文件位置成功！",
                                            data:result
                                        }
                                    })
                                }else{
                                    res.send({
                                        res:{
                                            ok:0,
                                            msg:"没找到该文件的位置！"
                                        }
                                    })
                                }
                            }
                        })
                    }else{
                        res.send({
                            res:{
                                ok:0,
                                msg:"没找到该文件的位置！"
                            }
                        })
                    }
                }
            });
            connection.release();
        }
    });
}


module.exports.query_location = query_location;